package com.gitee.sop.layui.adminserver.module.background.admin.view;

import com.gitee.sop.layui.adminserver.entrance.bo.ServiceContext;
import com.gitee.sop.layui.adminserver.module.background.admin.entity.SysApplication;
import com.gitee.sop.layui.adminserver.module.background.admin.entity.SysUser;
import com.gitee.sop.layui.adminserver.module.background.admin.service.SysApplicationService;
import com.gitee.sop.layui.adminserver.module.base.view.BaseViewController;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * @author: huwei
 * @date: 2019/11/13 15:05
 * @version: 1.0.0
 */
@Controller
@RequestMapping(value = {"/admin/sysApplication"})
public class SysApplicationViewController extends BaseViewController {
    private static final String VIEW_PAGE_PREFIX = "module/admin/sysApplication";
    @Lazy
    @Resource
    private SysApplicationService sysApplicationService;

    @Override
    protected String prefix() {
        return VIEW_PAGE_PREFIX;
    }

    @RequestMapping(value = {"/to/assign/{userUuid}"})
    public ModelAndView toApplicationAssign(@PathVariable("userUuid") String userUuid, ModelAndView modelAndView) {
        modelAndView.addObject("userUuid", userUuid);
        modelAndView.setViewName(prefix().concat("/applicationAssign.html"));
        return modelAndView;
    }

    /**
     * 去到应用选择页面
     *
     * @param modelAndView
     * @return
     */
    @RequestMapping(value = {"/to/application/selector"})
    public ModelAndView toApplicationSelector(ModelAndView modelAndView) {
        //获取用户关联应用，即用户可进入的应用
        SysUser user = ServiceContext.me().checkedUserAndGet(SysUser.class);
        List<SysApplication> sysApplicationList;
        synchronized (user.getUuid()) {
            sysApplicationList = ServiceContext.me().getRelationApplication();
            if (sysApplicationList == null) {
                sysApplicationList = sysApplicationService.getRelationListByUserUuid(user.getUuid());
                ServiceContext.me().setRelationApplication(sysApplicationList);
            }
        }
        modelAndView.setViewName(prefix().concat("/applicationSelector.html"));
        return modelAndView;
    }

    /**
     * 去到应用模板选择页
     *
     * @param modelAndView
     * @return
     */
    @RequestMapping(value = {"/to/application/template"})
    public ModelAndView toApplicationTemplate(ModelAndView modelAndView) {
        modelAndView.setViewName(prefix().concat("/applicationTemplate.html"));
        return modelAndView;
    }

    @Override
    public ModelAndView add(ModelAndView modelAndView, Long id, String key, HttpServletRequest request, HttpServletResponse response) {
        modelAndView.addObject("parentAppId", request.getParameter("parentAppId"));
        return super.add(modelAndView, id, key, request, response);
    }
}
